package cnpm02.j2ee.group3.logic.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;
import javax.persistence.EntityExistsException;

import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;

import cnpm02.j2ee.group3.logic.dao.impl.CityDao;
import cnpm02.j2ee.group3.logic.dao.impl.CityTranslationDao;
import cnpm02.j2ee.group3.logic.dao.impl.FlightLineDao;
import cnpm02.j2ee.group3.logic.exception.TwoFieldsMustBeDifferent;
import cnpm02.j2ee.group3.logic.utils.ATSMUtils;
import cnpm02.j2ee.group3.model.City;
import cnpm02.j2ee.group3.model.CityTranslation;
import cnpm02.j2ee.group3.model.FlightLine;

@Name("flightLine")
public class NewFlightLine implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private List<SelectItem> citys = new ArrayList<SelectItem>();
	
	@In
	private CityDao cityDao;
	@In
	private CityTranslationDao cityTranslationDao;
	@In
	private ATSMUtils atsmUtils;
	@In
	private FlightLineDao flightLineDao;
	private String selectedFromCity;
	private String selectedToCity;
	
	private List<FlightLine> flightLines;
	
	private FlightLine selectedFlightLine;
		
	@Create
	public void init()
	{
		//Initialize cities
		for (CityTranslation c : cityTranslationDao.selectAllByLocale(atsmUtils
				.getCurrentLanguage())) {
			citys.add(new SelectItem(String.valueOf(c.getCity().getId()), c
					.getTranslation()));
		}
	}
	
	public String doSave() throws EntityExistsException, TwoFieldsMustBeDifferent
	{
		//City from
		City fromCity = cityDao.find(Long.valueOf(getSelectedFromCity()));
		//City to
		City toCity = cityDao.find(Long.valueOf(getSelectedToCity()));
		//Return flightLine
		
		if(fromCity.equals(toCity)) {
			throw new TwoFieldsMustBeDifferent();
		}
		
		if(flightLineDao.selectFlightLineByFromAndTo(fromCity, toCity) != null) {
			throw new EntityExistsException();
		}
		
		FlightLine flightLine = new FlightLine();
		flightLine.setFrom(fromCity);
		flightLine.setTo(toCity);
		flightLineDao.saveOrUpdate(flightLine);
		
		FlightLine returnFlightLine = new FlightLine(toCity, fromCity, flightLine);
		flightLine.setReturnFlight(returnFlightLine);
		
		flightLineDao.saveOrUpdate(flightLine);
		//Refresh 
		flightLines = null;
		return "success";
	}

	public void doDelete() {
		flightLineDao.delete(selectedFlightLine);
		
		//Refresh
		flightLines = null;
	}
	
	public void setCitys(List<SelectItem> citys) {
		this.citys = citys;
	}

	public List<SelectItem> getCitys() {
		return citys;
	}

	public void setSelectedToCity(String selectedToCity) {
		this.selectedToCity = selectedToCity;
	}

	public String getSelectedToCity() {
		return selectedToCity;
	}

	public void setSelectedFromCity(String selectedFromCity) {
		this.selectedFromCity = selectedFromCity;
	}

	public String getSelectedFromCity() {
		return selectedFromCity;
	}

	public List<FlightLine> getFlightLines() {
		if(flightLines == null) {
			flightLines = flightLineDao.selectAll();
		}
		
		return flightLines;
	}

	public void setSelectedFlightLine(FlightLine selectedFlightLine) {
		this.selectedFlightLine = selectedFlightLine;
	}
}
